package main

import (
	util1 "GoStudy/src/go_code/project01/utils" //改名util
	"fmt"
	"strconv"
	"time"
	"unsafe"
)

var (
	e    = "e"                        //全局变量  不适用也可编译通过
	fun1 = func(n1 int, n2 int) int { //全局匿名函数
		return n1 * n2
	}
)

func main() {
	fmt.Printf("aaaaa \" \n")
	// \n换行 \r回车
	/* 多行注释
	多行注释 */

	var a string
	a = "1"
	b := "abcd" //声明变量
	var c, d = "c", "d"
	f := 1.2 //默认float64
	f++
	g := .1 //0.1
	g--
	h := 5.1e2 //5.1 * 10^2
	//h := 5.1-2 //5.1 / 10^2
	h++
	fmt.Printf(a + b + c + d + e)
	fmt.Printf("%T \n", a)                //查看变量类型
	fmt.Printf("%d \n", unsafe.Sizeof(f)) //查看变量占用字节数
	//int默认值0
	//var g bool = true
	//rune = int32 (4B)
	//	complex64()

	var i byte = 'a'
	fmt.Printf("c1=%v \n", i) //直接输出为对应的字符
	fmt.Printf("c1=%c \n", i) //格式化输出字符
	str := `sdfdsf`           //反引号  不支持转义
	fmt.Printf(str)

	var j int32 = 100
	var k float32 = float32(j)
	fmt.Printf("%d,%f \n", j, k)

	var l = 1111
	m := fmt.Sprintf("%d \n", l) //int转为String
	fmt.Printf("%v \n", m)
	n := strconv.FormatInt(int64(l), 10) //int转为String 10进制
	fmt.Printf("%v \n", n)
	var p = 11.11
	o := strconv.FormatFloat(p, 'f', 10, 64) //float转为String 'f'格式 保留10位 float64
	fmt.Printf("%v \n", o)
	var q = "123123"
	var r, _ = strconv.ParseInt(q, 10, 64) //10进制 int64
	fmt.Printf("%d \n", r)

	var s int = 10
	var ptr *int = &s
	fmt.Printf("%v \n", ptr)  //指针本身
	fmt.Printf("%v \n", &ptr) //指向的地址
	fmt.Printf("%v \n", *ptr) //指向的值

	var userStr = "aaaa" //同包可访问
	var UserStr = "bbbb" //他包可访问
	fmt.Printf(userStr + UserStr)
	s++ //自增自减运算符不可赋值与直接判断   无++s
	//^位异

	var name string
	var age int
	fmt.Scanln(&name)
	fmt.Scanf("%s %d", &name, &age)

	if true {

	} else if false {

	} else {

	}

	switch a { //默认自带break
	case "1", "2":
		fallthrough //穿透
	case "3":

	default:

	}

	var v = 3
	switch { //不带表达式约等于if else
	case v == 3:
	case v == 4:
	}
	str2 := []rune(str) //切片
	for w := 1; w < 4; w++ {
		fmt.Printf("%c \n", str2[w])
	}

	for v < 4 {
		v++
	}

	for { //死循环
		break
	}

	for index, val := range str { //for range  遍历中文时index会跳跃
		fmt.Printf("%d,%c", index, val) //循环取出下标与对应值
	}

label1:
	for { //for+if实现while
	label2:
		for {
			if v < 4 {
				break label1 //指定跳出哪层循环
			} else {
				break label2
			}
		}
	}

	if v > 0 {
		goto label1 //goto指定跳转到某一行
	}

	a1, a2 := aa(2) //函数
	fmt.Printf("%d %v \n", a1, a2)

	//传进函数的变量的值不会被直接更改  指针可改
	abc := util1.Abc()
	abc++

	type myInt int //自定义数据类型myInt
	var a3 myInt
	fmt.Printf("%T", a3)

	type myFucType func(int) int

	res1 := func(n1 int, n2 int) int { //匿名函数直接赋值
		return n1 + n2
	}(10, 20)
	fmt.Printf("%d", res1)

	now := time.Now()
	println(now)
	println(now.Format("2006/01/02 15:04:05"))
	println(now.Format("2006-01-02"))
	println(now.Format("15:04:05"))
	duration := 100 * time.Second
	duration1 := 100 * time.Millisecond
	duration2 := 100 * time.Microsecond //微秒
	duration3 := 100 * time.Nanosecond  //纳秒
	println(duration, duration1, duration2, duration3)
	unix := now.Unix()     //时间戳
	nano := now.UnixNano() //时间纳秒戳
	println(unix)
	println(nano)

	a5 := new(int) //a5为指针类型
	println(*a5)

	var asf [6]int
	//var asfd = [3]int{1, 2, 3}
	//var asfd = [...]int{0: 1, 1: 2, 2: 3}

	for index, val := range asf {
		println(index + val)
	}

	//数组值类型 切片引用类型
	var slice1 = []int{1, 2, 3} //切片数组可变
	//var slice2 = make([]int, 4, 10) //长度 < 容量   //使用make时切片连接的数组不可见
	//var slice3 = 1
	println(cap(slice1)) //容量初始值 增加多个元素容量不够时容量单次增
	slice1 = append(slice1, 4)

	//var adfsd [6][6]int    //二维数组

	var map1 = make(map[int]string, 10) //map需要make进行内存分配创建  //长度可不写自增
	map1[1] = "a"
	map1[2] = "b"
	var map2 = map[int]string{
		1: "a",
		2: "b",
	}
	delete(map2, 1) //没法直接删除所有

	for k, v := range map2 {
		println(k, v)
	}

	i2 := make([]map[string]string, 2)
	i2[0] = make(map[string]string)
	i2[0]["key1"] = "a"
	i2[0]["key2"] = "b"
	i2[1] = make(map[string]string)
	i2[1]["key1"] = "c"
	i2[1]["key2"] = "d"

}

func aa(int) (int, string) {
	return 1, "2"
}

func bb(int) (returnNum int) { //为返回值命名
	bbb := bb //函数赋值给变量
	fmt.Printf("%T", bbb)
	returnNum = 1
	return //给返回值命名后可直接return
}

func sum(myArgs ...int) (sumNum int) { //可变参数  只能写在形参列表最后
	for i := 0; i < len(myArgs); i++ {
		sumNum += myArgs[i]
	}
	return
}

func init() { //每个文件一个初始化函数 在main之前执行

}

func deferFunc() {
	var n = 1
	defer println("defer", n) //1
	/* 内容进入(defer)栈在函数最后执行
	   栈内先入后出    n的值不受后面操作影响   */
	n++
	println("first", n) //2
}
